package com.tenglong.teacher;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.cuotibao.zhuoxiao.basics.MyFragmentPagerAdapter;
import com.cuotibao.zhuoxiao.basics.utils.IpUtils;
import com.cuotibao.zhuoxiao.basics.utils.ToastUtils;
import com.netease.nim.uikit.common.ui.drop.DropManager;
import com.tenglong.teacher.data.resultInfo.DataState;
import com.tenglong.teacher.ui.port.MainCallback;
import com.tenglong.teacher.ui.port.MainFragmentRubsh;
import com.tenglong.teacher.ui.answerq.FragmentAnswerQ;
import com.tenglong.teacher.ui.common.activity.BaseActivity;
import com.tenglong.teacher.ui.homepage.FragmentHompage;
import com.tenglong.teacher.ui.homeworkNew.HomeWorkFragment;
import com.tenglong.teacher.ui.notiUtil.NotificationModule;
import com.tenglong.teacher.ui.port.MainMessageListener;
import com.tenglong.teacher.ui.setting.FragmentSetting;


import java.util.Objects;

public class MainActivity extends BaseActivity implements MainCallback {
    private ViewPager mViewPager;
    private RadioGroup mRadioGroup;
    private long mExitTime = 0;
    private View userPoint, homeorkPoint, answerPoint, homePagerPoint;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setListeners();
        setFragment();
        MyApplication.registerNotificationObserver();
    }

    @SuppressLint("NewApi")
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (intent != null) {
            int type = intent.getIntExtra(NotificationModule.KEY_SKIP_ACTIVITY_TYPE, 0);
            if (type == NotificationModule.NOTIFICATION_HOMEWORK) {
                ((RadioButton) mRadioGroup.findViewById(R.id.radio_three)).setChecked(true);
                FragmentSwitcher homeworkFragemnt = (FragmentSwitcher) getFragment(2);
                if (Objects.requireNonNull(homeworkFragemnt).onGetFragmentPosition() != 0 && Objects.requireNonNull(homeworkFragemnt).onGetFragmentPosition() != -1) {
                    homeworkFragemnt.onSwitcher(0);
                } else {
                    ((MainFragmentRubsh) homeworkFragemnt).refresh();
                }
            } else if (type == NotificationModule.NOTIFICATION_VERIFY_MESSAGE) {
                MainMessageListener userFragemnt = (MainMessageListener) getFragment(3);
                if (userFragemnt != null) {
                    userFragemnt.skipVerifyMessage();
                }
            }
        }
    }

    private void setListeners() {
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                tabSwitcher(checkedId);
            }
        });
    }

    private void initView() {
        setTitle("主页");
        findViewById(R.id.actionbar_container).setVisibility(View.GONE);
        mViewPager = findViewById(R.id.viewPager);
        mViewPager.setOffscreenPageLimit(3);
        mRadioGroup = findViewById(R.id.mRadioGroup);
        userPoint = findViewById(R.id.userPoint);
        homeorkPoint = findViewById(R.id.homeorkPoint);
        answerPoint = findViewById(R.id.answerPoint);
        homePagerPoint = findViewById(R.id.homePagerPoint);
        if (this.mBack != null) {
            this.mBack.setVisibility(View.GONE);
        }
    }


    @Override
    public void onBackPressed() {
        if ((System.currentTimeMillis() - mExitTime) > 2000) {
            ToastUtils.showCenterToast("再按一次退出");
            mExitTime = System.currentTimeMillis();
        } else {
            DataState.getInstance().appExit();
            super.onBackPressed();
        }
    }

    private void setFragment() {
        MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
        for (int i = 0; i < 4; i++) {
            adapter.addFragment(createFragment(getSupportFragmentManager(), i));
        }
        mViewPager.setAdapter(adapter);
    }

    @SuppressLint("NewApi")
    private void tabSwitcher(int checkedId) {
        switch (checkedId) {
            case R.id.radio_one:
                mViewPager.setCurrentItem(0);
                Objects.requireNonNull(getFragment(0)).refresh();
                break;
            case R.id.radio_two:
                mViewPager.setCurrentItem(1);
                Objects.requireNonNull(getFragment(1)).refresh();
                break;
            case R.id.radio_three:
                mViewPager.setCurrentItem(2);
                Objects.requireNonNull(getFragment(2)).refresh();
                break;
            case R.id.radio_four:
                mViewPager.setCurrentItem(3);
                Objects.requireNonNull(getFragment(3)).refresh();
                break;
        }
    }


    private MainFragmentRubsh getFragment(int index) {
        Fragment fragment = createFragment(getSupportFragmentManager(), index);
        return fragment instanceof MainFragmentRubsh ? (MainFragmentRubsh) fragment : null;
    }


    private Fragment createFragment(FragmentManager manager, int position) {
        Fragment fragment = manager.findFragmentByTag("android:switcher:" + mViewPager.getId() + ":" + position);
        switch (position) {
            case 0:
                return fragment == null ? new FragmentHompage() : fragment;
            case 1:
                return fragment == null ? new FragmentAnswerQ() : fragment;
            case 2:
                return fragment == null ? new HomeWorkFragment() : fragment;
            default:
                return fragment == null ? new FragmentSetting() : fragment;
        }
    }

    @Override
    public void onUserRedPoint(boolean isShow) {
        userPoint.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onHomeworkRedPoint(boolean isShow) {
        homeorkPoint.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onAnswerRedPoint(boolean isShow) {
        answerPoint.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onHomePagerRedPoint(boolean isShow) {
        homePagerPoint.setVisibility(isShow ? View.VISIBLE : View.GONE);
    }

    @Override
    public void onUserRefresh() {
        getFragment(3).refresh();
    }

    @Override
    public void onHomeworkRefresh() {
        getFragment(2).refresh();
    }

    @Override
    public void onAnswerRefresh() {
        getFragment(1).refresh();
    }

    @Override
    public void onHomePagerRefresh() {
        getFragment(0).refresh();
    }


    public interface FragmentSwitcher {
        void onSwitcher(int position);

        int onGetFragmentPosition();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DropManager.getInstance().destroy();
        MyApplication.unRegisterNotificationObserver();
    }
}

